package com.xzl.xep.web.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xzl.xep.common.utils.DateUtil;
import com.xzl.xep.common.utils.StringUtil;
import com.xzl.xep.exception.BusinessException;
import com.xzl.xep.pojo.Customer;
import com.xzl.xep.service.CustomerService;
import com.xzl.xep.service.ReportService;



@Controller
@RequestMapping(value ="/customer" )
public class CustomerController extends BaseController {
	
	@SuppressWarnings("unused")
	private static final Logger logger = LoggerFactory.getLogger(BaseController.class);

	@Autowired
	private CustomerService service;
	
	@Autowired
	private ReportService report;
	
	
	 /**
	 * 新增用户界面
	 */
	@RequestMapping(value ="new", method=RequestMethod.GET )
	public String newCunstomer() {
		return "customer/new";
	}
	 
	 
	/**
	 * 用户管理界面
	 */
	@RequestMapping(value ="", method=RequestMethod.GET )
	public String customer(Model model) {
		
		List<Customer> customers = service.getCustomers(companyId);
		
		model.addAttribute("customerList",customers );
		model.addAttribute("today", report.getTodayDto(companyId));
		model.addAttribute("total", report.getTotalDto(companyId));
		
		return "customer/manage";
	}
	
	

	 /**
	 * 新增用户
	 */
	@RequestMapping(value ="/new", method=RequestMethod.POST )
	@ResponseBody
	public Map<String,Object> newCunstomer(Customer customer, Integer way) {
		Map<String,Object> result = new HashMap<String,Object>();
		if(StringUtil.isEmpty(customer.getCardno()))
			throw new BusinessException("卡号不能为空！");
		
		if(!service.checkCustomerExists(customer))
			throw new BusinessException("卡号或手机已存在！");
		
		result.put("customer", service.addCustomer(customer,way,operator));
		
		result.put("totalCredit", report.getCredit(companyId, null, null));
		result.put("todayCredit", report.getCredit(companyId, DateUtil.getCurrentDate(), DateUtil.getCurrentDate()));
		result.put("todayCounts", report.getCustomerCounts(companyId, DateUtil.getCurrentDate(), DateUtil.getCurrentDate()));
		result.put("totalCounts", report.getCustomerCounts(companyId, null, null));
		
		return result;
	}
	 
	
	@RequestMapping(value ="/queryMobile", method=RequestMethod.POST )
	@ResponseBody
	public List<Customer> queryMobile(String term) {
		String mobile = term.replace("_", "").trim();
		
		
		return service.queryMobile(companyId,mobile);
	}
	
	@RequestMapping(value ="/queryCardno", method=RequestMethod.POST )
	@ResponseBody
	public List<Customer> queryCardno(String term) {
		return service.queryCardno(companyId,term);
	} 
	
	
	@RequestMapping(value ="/credit", method=RequestMethod.POST )
	@ResponseBody
	public Map<String,Object> credit(Long customerId, Integer way, Double money) {
		
		Map<String,Object> result = new HashMap<String,Object>();
		if(customerId == null || customerId ==0)
			throw new BusinessException("用户不能为空！");
		
		if(money == null || money == 0)
			throw new BusinessException("充值金额不能为空！");
		
		service.credit(operator,customerId,way,money);
		 
		
		result.put("totalCredit", report.getCredit(companyId, null, null));
		result.put("todayCredit", report.getCredit(companyId, DateUtil.getCurrentDate(), DateUtil.getCurrentDate()));

		
		return result;
	} 
	
}
